package com.kyzh.core.adapters

import android.view.View
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.ui.ArcButton
import com.gushenge.core.beans.Deal
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.impls.WealImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.activities.BaseFragmentActivity
import com.kyzh.core.uis.alert
import com.kyzh.core.utils.loading
import com.kyzh.core.utils.startActivity
import com.gushenge.core.toast


class MyDealAdapter(layout: Int, val beans: ArrayList<Deal>) : BaseQuickAdapter<Deal, BaseViewHolder>(layout, beans) {
    override fun convert(helper: BaseViewHolder, item: Deal) {
        item.apply {
            helper.setText(R.id.time, style)
                    .setText(R.id.name, name)
                    .setText(R.id.content, introduction)
                    .setText(R.id.price, "¥${money}")
                    helper.getView<ArcButton>(R.id.abDelete).setOnClickListener {
                        context.alert("提示","确定删除此订单吗？","删除","取消",{
                        val loading = context.loading()
                            WealImpl.dealDelete(id, object : ResultListener {
                                override fun success(bean: Any) {
                                    loading.dismiss()
                                    bean.toast()
                                    remove(helper.adapterPosition)
                                    notifyDataSetChanged()
                                }

                                override fun error(error: String) {
                                    loading.dismiss()
                                    error.toast()
                                }
                            })
                        },{

                        })

                    }

            helper.itemView.setOnClickListener {
                context.startActivity<BaseFragmentActivity>(
                        GlobalKeys.TYPE to BaseFragmentActivity.DEAL_PRODUCT_DETAIL,
                        GlobalKeys.KEY to beans[helper.adapterPosition].id
                )
            }
            val type = helper.getView<TextView>(R.id.type)
            type.visibility = View.VISIBLE
            type.text = this.type
            Glide.with(context).load(image).into(helper.getView(R.id.image))
        }
    }
}